<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019\03\11 0002
 * Time: 14:57
 */

namespace think\upush;

use Think\Exception;

require_once './../plugins/UPush/android/AndroidCustomizedcast.php';
require_once './../plugins/UPush/android/AndroidBroadcast.php';
require_once './../plugins/UPush/android/AndroidFilecast.php';
require_once './../plugins/UPush/android/AndroidGroupcast.php';
require_once './../plugins/UPush/android/AndroidUnicast.php';
require_once './../plugins/UPush/ios/IOSBroadcast.php';
require_once './../plugins/UPush/ios/IOSFilecast.php';
require_once './../plugins/UPush/ios/IOSGroupcast.php';
require_once './../plugins/UPush/ios/IOSUnicast.php';
require_once './../plugins/UPush/ios/IOSCustomizedcast.php';

class UPush
{
    protected $android_appkey;
    protected $android_appMasterSecret;
    protected $ios_appkey;
    protected $ios_appMasterSecret;
    protected $timestamp = null;
    protected $validation_token = null;

    public function __construct()
    {
        $this->timestamp = strval(time());
        $this->android_appkey = config('app.UPush.AndroidAppkey');
        $this->android_appMasterSecret = config('app.UPush.AndroidAppMasterSecret');
        $this->ios_appkey = config('app.UPush.IosAppkey');
        $this->ios_appMasterSecret = config('app.UPush.IosAppMasterSecret');
    }

    /**
     * Notes: 安卓推送-广播
     * User: Administrator
     * Date: 2018\5\2 0002
     * Time: 16:32
     * @param $title
     * @param $content
     * @return mixed
     * @throws \Exception
     */
    public function sendAndroidBroadcast($title, $content, $extra)
    {
        try {
            $brocast = new \AndroidBroadcast();
            $brocast->setAppMasterSecret($this->android_appMasterSecret);
            $brocast->setPredefinedKeyValue("appkey", $this->android_appkey);
            $brocast->setPredefinedKeyValue("timestamp", $this->timestamp);
            $brocast->setPredefinedKeyValue("ticker", "Android broadcast ticker");
            $brocast->setPredefinedKeyValue("title", $title);
            $brocast->setPredefinedKeyValue("text", $content);
            $brocast->setPredefinedKeyValue("after_open", "go_app");
            $brocast->setPredefinedKeyValue("production_mode", "false");
            $brocast->setExtraField("message", $extra);
            return $brocast->send();
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    /**
     * Notes: 安卓自定义消息
     * User: Administrator
     * Date: 2018\5\2 0002
     * Time: 17:59
     * @param $tokens
     * @param $message
     * @return mixed
     * @throws \Exception
     */
    public function sendAndroidUnicastMessage($tokens, $message)
    {
        try {
            $unicast = new \AndroidUnicast();
            $unicast->setAppMasterSecret($this->android_appMasterSecret);
            $unicast->setPredefinedKeyValue("appkey", $this->android_appkey);
            $unicast->setPredefinedKeyValue("timestamp", $this->timestamp);
            $unicast->setPredefinedKeyValue("device_tokens", $tokens);
            $unicast->setPredefinedKeyValue("production_mode", "false");
            $unicast->setPredefinedKeyValue("display_type", "message");
            $unicast->setPredefinedKeyValue("custom", $message);
            return $unicast->send();
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    /**
     * Notes: 安卓推送-单播
     * User: Administrator
     * Date: 2018\5\2 0002
     * Time: 15:36
     * @param $title
     * @param $content
     * @param $tokens
     * @return mixed
     * @throws
     */
    public function sendAndroidUnicast($title, $content, $tokens, $extra)
    {
        try {
            $unicast = new \AndroidUnicast();
            $unicast->setAppMasterSecret($this->android_appMasterSecret);
            $unicast->setPredefinedKeyValue("appkey", $this->android_appkey);
            $unicast->setPredefinedKeyValue("timestamp", $this->timestamp);
            $unicast->setPredefinedKeyValue("device_tokens", $tokens);
            $unicast->setPredefinedKeyValue("ticker", "Android unicast ticker");
            $unicast->setPredefinedKeyValue("title", $title);
            $unicast->setPredefinedKeyValue("text", $content);
            $unicast->setPredefinedKeyValue("after_open", "go_app");
            $unicast->setPredefinedKeyValue("production_mode", "false");
            $unicast->setExtraField("message", $extra);
            return $unicast->send();
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    /**
     * Notes: ios 广播
     * User: Administrator
     * Date: 2018\5\2 0002
     * Time: 15:37
     * @param $title
     * @param $content
     * @return mixed
     * @throws
     */
    public function sendIOSBroadcast($title, $content)
    {
        try {
            $brocast = new \IOSBroadcast();
            $brocast->setAppMasterSecret($this->ios_appMasterSecret);
            $brocast->setPredefinedKeyValue("appkey", $this->ios_appkey);
            $brocast->setPredefinedKeyValue("timestamp", $this->timestamp);
            $brocast->setPredefinedKeyValue("alert", $title);
            $brocast->setPredefinedKeyValue("badge", 0);
            $brocast->setPredefinedKeyValue("sound", "chime");
            $brocast->setPredefinedKeyValue("content-available", 1);
            $brocast->setPredefinedKeyValue("production_mode", "false");
            $brocast->setCustomizedField("message", $content);
            return $brocast->send();
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    /**
     * Notes: ios单播
     * User: Administrator
     * Date: 2019\3\11 0002
     * Time: 15:38
     * @param $title json  title subtitle  body
     * @param $content
     * @param $tokens
     * @return mixed
     * @throws
     */
    public function sendIOSUnicast($title, $content, $tokens)
    {
        try {
            $unicast = new \IOSUnicast();
            $unicast->setAppMasterSecret($this->ios_appMasterSecret);
            $unicast->setPredefinedKeyValue("appkey", $this->ios_appkey);
            $unicast->setPredefinedKeyValue("timestamp", $this->timestamp);
            $unicast->setPredefinedKeyValue("device_tokens", $tokens);
            $unicast->setPredefinedKeyValue("alert", $title);
            $unicast->setPredefinedKeyValue("badge", 0);
            $unicast->setPredefinedKeyValue("sound", "chime");
            $unicast->setPredefinedKeyValue("production_mode", "false");
            $unicast->setCustomizedField("message", $content);
            return $unicast->send();
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }
}